<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增资源
            </el-header>
            <el-main>
                <el-form ref="resData" :rules="rules" :model="resData" label-width="115px"
                         :label-position="formAlignLeft">
                    <el-form-item label="资源编码 :" prop="code">
                        <el-input v-model="resData.code" placeholder="请输入资源名称"></el-input>
                    </el-form-item>
                    <el-form-item label="资源名称 :" prop="name">
                        <el-input v-model="resData.name" placeholder="请输入资源名称"></el-input>
                    </el-form-item>
                    <el-form-item label="资源URL地址 :" prop="url">
                        <el-input v-model="resData.url" placeholder="请输入资源URL地址"></el-input>
                    </el-form-item>
                    <el-form-item label="资源是否启用 :" prop="enabled">
                        <el-radio-group v-model="resData.enabled">
                            <el-radio :label="true">启用</el-radio>
                            <el-radio :label="false">不启用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        完成新增
                    </el-button>
                    <el-button @click="editOut">
                        取消新增
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addRes",
        data() {
            return {
                formAlignLeft: 'left',
                resData: {
                    code: '',
                    name: '',
                    url: '',
                    enabled: false
                },
                rules: {
                    code: [
                        {required: true, message: '资源编码不得为空', trigger: 'blur'},
                    ],
                    name: [
                        {required: true, message: '资源名称不得为空', trigger: 'blur'},
                    ],
                    url: [
                        {required: true, message: 'URL地址不得为空', trigger: 'blur'},
                    ],
                    enabled: [
                        {required: true, message: '此选项为必选选项', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            };
        },
        mounted() {
        },
        methods: {
            submit() {
                this.$refs['resData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/res",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.resData)
                    }).then(response => {
                        this.info = '新增成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.cleanRes();
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = "新增失败";
                        this.infoShow = true;
                    });
                });
            },
            cleanRes() {
                this.resData = {
                    code: '',
                    name: '',
                    url: '',
                    enabled: false
                }
            },
            editOut() {
                this.infoShow = false;
                this.cleanRes();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 360px;
        height: 100%;
        z-index: 100;
    }

    .title {
        width: 100%;
        height: 4%;
        border-bottom: 1px solid rgba(238, 238, 238, 1);
        display: flex;
        font-size: 18px;
        font-weight: 400;
        color: rgba(49, 49, 49, 1);
    }

    .body {
        width: 100%;
        height: 82%;
        padding-top: 10px;
        overflow-y: auto;
    }
</style>